use std::fs::File;
use std::io::{self, prelude::*, BufReader};

fn main() -> io::Result<()>{

    let input = File::open("input.txt")?;
    let reader = BufReader::new(input);
    let mut sum_of_any_yes = 0;
    let mut sum_of_all_yes = 0;
    let mut question_any_yes: Vec<char> = Vec::new();
    let alphabet:Vec<char> = (97..=122).map(|c:u8| c as char).collect();
    let mut question_all_yes: Vec<char> = Vec::new(); 
    question_all_yes.append(&mut alphabet.to_vec());
    for line in reader.lines() {
        let str = line?;
        if question_any_yes.len() != 0 && (str.len() == 0){
            question_any_yes.sort_unstable();
            question_any_yes.dedup();
            sum_of_any_yes += question_any_yes.len();
            sum_of_all_yes += question_all_yes.len();
            question_any_yes.clear();
            question_all_yes.clear();
            question_all_yes.append(&mut alphabet.to_vec());
        }else{
            question_any_yes.append(&mut (str.chars().collect::<Vec<char>>()));
            //let invalid_question = question_all_yes.iter().filter( |&r| !str.contains(*r) );
            let mut i = 0;
            while i != question_all_yes.len() {
                if !str.contains(question_all_yes[i]) {
                    question_all_yes.remove(i);
                } else {
                    i += 1;
                }
            }
        }
    }

    question_any_yes.sort_unstable();
    question_any_yes.dedup();
    sum_of_any_yes += question_any_yes.len();
    sum_of_all_yes += question_all_yes.len();

    println!("Part 1 answer : {}", sum_of_any_yes);
    println!("Part 2 answer : {}", sum_of_all_yes);

    Ok(())
}
